//
// Created by jinling on 2025/6/25.
//

#include"LogStream.h"

using namespace  tmms::base;

static  thread_local pid_t thread_id =0;

const char* log_string[] = {
    "TRACE", "DEBUG", "INFO", "WARN","ERROR"
};


namespace tmms::base {
    Logger* g_logger = nullptr; // 初始化为nullptr避免未定义行为 ，，他是个单例模式的类，
}

LogStream::LogStream(Logger* logger, const char* file, int line, LogLevel level, const char* func) :logger_(logger)
{
    const char  * file_name = strrchr(file,'/');//该/最后一次出现位置的指针。得出文件名
    if (file_name)
    {
        file_name=file_name + 1;
    }
    else
    {
        file_name = file;
    }
    stream_<<TTime::ISOTime();
    if (thread_id == 0) {
        thread_id = static_cast<pid_t>(::syscall((SYS_gettid)));
    }

    stream_<< thread_id;
    stream_ << log_string[level];//输出打印级别
    stream_ << "["<<file_name<<":"<<line<<"]";//文件名加行数
    if (func)
    {
        stream_<<"["<<func<<"]";
    }
}

LogStream::~LogStream() //在析构函数里面打印
{
    stream_<<"\n";
    logger_->Write(stream_.str());
}



